package com.fingard.dsp.bank.directbank.bocom13;

import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOCOM13DeContract extends BOCOM13Base implements IDeContract {

    /**
     * 参考文档：交通银行资金清算平台——联机交易V4.4
     * 实现接口：客户代收协议撤约（I00409）
     **/
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;

        try {
            BOCOM13Header tmpBankHeader = new BOCOM13Header();
            tmpBankHeader.reqSeqID = p_req.contractDetail.askNumber;
            String corpNo = getBankConfig().getMapValueByKey("corpNoMapping", p_req.contractDetail.recAct);
            if (StringHelper.isNullOrEmpty(corpNo)) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req.contractDetail.recAct + "]保险方ID映射,请检查BOCOM13/config.xml中'corpNoMapping'节点配置";
                tmpRetDetail.deSignState = ContractState.FAIL;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            tmpBankHeader.corpNo = corpNo;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            tmpBankHeader.transCode = "I00409";
            writeCommonHeader(xmlWriter, tmpBankHeader);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("Name", p_req.contractDetail.payName);//* 客户姓名 *//*
            xmlWriter.writeElementString("ActNo", p_req.contractDetail.payAct);//* 账号 *//*
            xmlWriter.writeElementString("AgreementNo", p_req.contractDetail.authCode);//* 交易币种 *//*
            xmlWriter.writeElementString("Ext001", "");
            xmlWriter.writeElementString("Ext002", "");
            xmlWriter.writeElementString("Ext003", "");
            xmlWriter.writeElementString("Ext004", "");
            xmlWriter.writeElementString("Ext005", "");
            xmlWriter.writeElementString("Ext006", "");
            xmlWriter.writeElementString("Ext007", "");
            xmlWriter.writeElementString("Ext008", "");
            xmlWriter.writeElementString("Ext009", "");
            xmlWriter.writeElementString("Ext010", "");
            xmlWriter.writeEndDocument();
            String[] tmpRetStr = sendToBank(xmlWriter.toXmlString(), bankFront.serverPort);
            if (tmpRetStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.respInfo = tmpBkReader.getFirstText("/Rsp/Head/ErrMsg");
                    String tmpMsgType = tmpBkReader.getFirstText("/Rsp/Head/MsgType");
                    String tmpErrCode = tmpBkReader.getFirstText("/Rsp/Head/ErrCode");
                    if (!"N".equals(tmpMsgType)) {
                        tmpRetDetail.respCode = "-1";
                    }
                    if ("000000".equals(tmpErrCode)) {
                        tmpRetDetail.deSignState = ContractState.SUCCESS;
                    } else {
                        tmpRetDetail.deSignState = ContractState.FAIL;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpRetStr[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}
